app.controller('BsShiftDetailCtrl', function ($scope, $stateParams, ajax, toaster, $modal) {

    $scope.shiftId = $stateParams.shiftId;

    $scope.reload = function () {
        ajax.call(new Req.GET({
            resource: 'shift/bs/ticket/' + $scope.shiftId + '/info'
        })).then(function (result) {
            if (result.code && result.code == 200) {
                $scope.shift = result.result.shift;
                $scope.tickets = result.result.tickets;
            }
        }, function () { });
    };

    $scope.reload();

    $scope.change = function (ticket) {
        $scope.current_data = angular.copy(ticket);
        if (ticket.locked && ticket.hold) {
            $modal.open({
                templateUrl: 'template/confirmModal.html?v=82a28a704b',
                controller: 'ModalCtrl',
                size: 'sm',
                resolve: {
                    param: function () {
                        return {
                            "data": $scope.current_data,
                            "title": "解锁预留车票",
                            "msg": "确定解锁预留的车票？"
                        };
                    }
                }
            }).result.then(function (param) {
                ajax.call(new Req.POST({
                    fields: param.data,
                    resource: 'shift/bs/ticket/{shiftId}/unhold'.format({ shiftId: $scope.shiftId })
                })).then(function (result) {
                    if (result && result.code == 200) {
                        ticket.locked = !ticket.locked;
                        ticket.hold = !ticket.hold;
                        toaster.pop('success', '成功', '车票已解锁。');
                    } else {
                        toaster.pop('error', '错误', result.result);
                    }
                }, function () {
                    toaster.pop('error', '错误', '连接错误，请求失败！');
                });
            });
        } else if (!ticket.locked && !ticket.hold) {
            $scope.current_data = angular.copy(ticket);
            $modal.open({
                templateUrl: 'template/confirmModal.html?v=82a28a704b',
                controller: 'ModalCtrl',
                size: 'sm',
                resolve: {
                    param: function () {
                        return {
                            "data": $scope.current_data,
                            "title": "预留车票",
                            "msg": "确定要锁定当前车票？"
                        };
                    }
                }
            }).result.then(function (param) {
                ajax.call(new Req.POST({
                    fields: param.data,
                    resource: 'shift/bs/ticket/{shiftId}/hold'.format({ shiftId: $scope.shiftId })
                })).then(function (result) {
                    if (result && result.code == 200) {
                        ticket.locked = !ticket.locked;
                        ticket.hold = !ticket.hold;
                        toaster.pop('success', '成功', '车票已锁定。');
                    } else {
                        toaster.pop('error', '错误', result.result);
                    }
                }, function () {
                    toaster.pop('error', '错误', '连接错误，请求失败！');
                });
            });
        }
    };

    $scope.rebuild = function () {
        $modal.open({
            templateUrl: 'template/confirmModal.html?v=82a28a704b',
            controller: 'ModalCtrl',
            size: 'sm',
            resolve: {
                param: function () {
                    return {
                        "data": $scope.current_data,
                        "title": "同步票池",
                        "msg": "确定同步票池？"
                    };
                }
            }
        }).result.then(function () {
            ajax.call(new Req.POST({
                resource: 'shift/bs/ticket/{shiftId}/rebuild'.format({ shiftId: $scope.shiftId })
            })).then(function (result) {
                if (result && result.code == 200) {
                    $scope.reload();
                    toaster.pop('success', '成功', '同步成功。');
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, function () {
                toaster.pop('error', '错误', '连接错误，请求失败！');
            });
        });
    };
});